﻿using AutoMapper;
using EFEntity;
using ISerives;
using Microsoft.EntityFrameworkCore;
using Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Serives
{
    public  class sfServices : IsfServices
    {
        private readonly MyDbContext myDbContext;
        private readonly IMapper mapper;
        public sfServices(MyDbContext myDbContext, IMapper mapper)
        {
            this.myDbContext = myDbContext;
            this.mapper = mapper;
        }

        public async Task<int> Add(Esf e)
        {
            myDbContext.Esf.Add(e);
            int num = await myDbContext.SaveChangesAsync();
            return num;
        }

        public async Task<int> Del(int id)
        {
            Esf d = new Esf()
            {
                id=id
            };
            myDbContext.Esf.Attach(d);
            myDbContext.Esf.Remove(d);
            int num = await myDbContext.SaveChangesAsync();
            return num;
        }

        public async Task<List<sf>> Select()
        {
            List<sf> list1 = new List<sf>();
            await Task.Run(() =>
            {
                List<Esf> list = myDbContext.Esf.ToList();
                list1 = mapper.Map<List<sf>>(list);

            });
            return list1;
        }

        public async Task<Esf> Up(int id)
        {
            Esf a = new Esf();
            await Task.Run(() =>
            {
                a = myDbContext.Esf.Where(e => e.id == id).FirstOrDefault();

            });
            return a;
        }

        public async Task<int> Update(Esf s)
        {
            myDbContext.Esf.Attach(s);
            myDbContext.Entry(s).State = EntityState.Modified;
            int num = await myDbContext.SaveChangesAsync();
            return num;
        }
    }
}
